package com.tycmc.signelectroniccontracts.tools.htmlcommit;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;


import com.tycmc.signelectroniccontracts.R;
import com.tycmc.signelectroniccontracts.tools.adapter.abslistview.AdapterHelper;
import com.tycmc.signelectroniccontracts.tools.adapter.abslistview.GeneralAdapter;

import java.util.List;

/**
 * Created by gy on 2017/11/7.
 */

public class PopHtml extends PopupWindow {


    private GeneralAdapter<HtCommitEntity.ChoseDataEntity> adapter;


    public PopHtml(Context context,List<HtCommitEntity.ChoseDataEntity> list, final OnItemListen onItemClickListener) {
        super(context);
        View inflate = ((LayoutInflater) context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.pop_bottom, null);
        ListView listView = (ListView) inflate.findViewById(R.id.listView);
        adapter = new GeneralAdapter<HtCommitEntity.ChoseDataEntity>(context, R.layout.item_pop_item,list) {
            @Override
            public void convert(AdapterHelper helper, HtCommitEntity.ChoseDataEntity item) {
                helper.setText(R.id.tv_name,item.getChosename());
            }
        };
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (onItemClickListener != null) {
                    onItemClickListener.itemOnclick(parent,position,view);
                }
                dismiss();
            }
        });
        this.setContentView(inflate);
        // 设置SelectPicPopupWindow弹出窗体的宽
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int screesnWidth = wm.getDefaultDisplay().getWidth();
        int screesnHeight = wm.getDefaultDisplay().getHeight();
        this.setWidth(screesnWidth);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(screesnHeight/2);

        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.pop_add_ainm);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
    }


    public interface OnItemListen{
        void itemOnclick(AdapterView<?> parent, int position, View view);
    }
    public void show(View view) {
        showAtLocation(view,
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }
}
